import machine

def __init__(self,name,gpio,mode='r',value=0):
    self.name = name
    self.instance = gpio

    #deal with inverted pins
    self.histate = 1
    if 'i' in mode:
        self.histate = 0
        value = not value

    self.mode = mode
    self.pwm = self.servo = None

    #print('self=',self,', pin=',pin,', mode=',mode,', value=',value, sep='')

    if 'w' in mode:
        #self.instance = machine.Pin(gpio,mode=machine.Pin.OUT,value=value)
        if 's' in mode:
            pdb('211:servo')
        if 'p' in mode:
            pdb('213:pwm')
    elif 'r' in mode:
        #self.instance = machine.Pin(gpio,mode=machine.Pin.IN)
        pass
    else:
        pdb('27: miss pin %s mode' % self.gpio, name )

def as_servo(self,left=0,neutral=90,right=180):
    self.servo = self.Servo()
    self.servo.call( [ self.instance] )
    self.servo.set_table(left,neutral,right)
    self.set_pos = self.servo.set_pos
    self.remap = self.servo.remap

    return self

def as_pwm(self,freq=50, duty=0,max=0):
    self.pwm_max = max
    self.pwm = machine.PWM(self.instance ,freq=freq, duty=duty)
    return self
